--[[
	FlameWarden UI: display which year the various fires were last desecrated in.
	
	To get our modified to display, inject a custom GetAchievementCriteriaInfo into
	the environment of AchievementObjectives_DisplayCriteria: this ensures minimal
	disruption to any other functional area.
]]

local env = setmetatable({}, {__index=_G});
local achievementCompleted, achievementID, cyear;

local aIco, hIco = [[|TInterface\Icons\Inv_Misc_SummerFest_BrazierBlue:22|t ]], [[|TInterface\Icons\Inv_Misc_SummerFest_BrazierRed:22|t ]];
if UnitFactionGroup("player") ~= "Alliance" then aIco, hIco = hIco, aIco; end
local function getIconPrefix(zone)
	local text, friendly, hostile = "", FlameWarden.GetZoneState(zone);
	cyear = select(4, CalendarGetDate());
	if friendly and friendly ~= cyear then
		text = aIco;
	end
	if hostile and hostile ~= cyear then
		text = text .. hIco;
	end
	return text;
end

local function process(id, ...)
	local name, ctype, lcompleted = ...;
	if achievementID ~= id then 
		achievementID, achievementCompleted = id, select(4, GetAchievementInfo(id));
	end
	local check = id > 1027 and FlameWarden.IsZoneDesecrated or FlameWarden.IsZoneHonoured;
	if type(check) == "function" then
		local year, color = check(name);
		if year and lcompleted then 
			cyear = select(4, CalendarGetDate());
			if year ~= cyear then
				color = achievementCompleted and "800000" or "ff8000";
			else
				color = achievementCompleted and "004000" or "00ff00";
			end
			return ("%s |cff%s(%s)|r"):format((...), color, year), select(2, ...);
		end
	end
	return ...;
end
function env.GetAchievementCriteriaInfo(...)
	local id, i = ...;
	if type(id) ~= "number" or id < 1022 or id > 1033 or i == nil then return GetAchievementCriteriaInfo(...); end
	return process(id, GetAchievementCriteriaInfo(...));
end
local function procTaxi(...)
	if type((...)) == "string" then
		local p2 = (...):match(", (.-)$");
		if p2 then return (...) .. getIconPrefix(p2):gsub("(|T.-|t) ", " %1"), select(2, ...); end
	end
	return ...;
end
function env.TaxiNodeName(...)
	return procTaxi(TaxiNodeName(...));
end

FlameWardenEventTracker:RegisterEvent("ADDON_LOADED");
function FlameWardenEventTracker:ADDON_LOADED(addon)
	if addon == "Blizzard_AchievementUI" then
		setfenv(AchievementObjectives_DisplayCriteria, env)
	end
end

local origSetText = MinimapZoneText.SetText;
local function zoneText(self, text)
	if type(text) == "string" then
		text = getIconPrefix(self == MinimapZoneText and GetRealZoneText() or text) .. text;
		if self ~= MinimapZoneText then
			text = text:gsub(":22", ":32");
		end
		origSetText(self, text);
	end
end

local hooked;
local function scanCalendar()
	if hooked then return; end
	local _, m, d, y = CalendarGetDate();
	CalendarSetAbsMonth(m,y);
	for i=1, CalendarGetNumDayEvents(0, d) do
		local etype = select(7,CalendarGetDayEvent(0, d, i));
		if etype == "Calendar_Midsummer" then
			hooksecurefunc(MinimapZoneText, "SetText", zoneText);
			hooksecurefunc(WorldMapFrameAreaLabel, "SetText", zoneText);
			hooksecurefunc(FlameWarden, "Update", Minimap_Update); 
			setfenv(TaxiNodeOnButtonEnter, env);
			Minimap_Update();
			hooked = true;
			return true;
		end
	end
end

OpenCalendar();
FlameWardenEventTracker:RegisterEvent("PLAYER_ENTERING_WORLD");
function FlameWardenEventTracker:PLAYER_ENTERING_WORLD()
	if not scanCalendar() then 
		local f,c = CreateFrame("Frame"), 200;
		f:SetScript("OnUpdate", function()
			c = c - 1;
			if c == 0 then
				scanCalendar();
				f:Hide();
			end
		end);
	end
	self:UnregisterEvent("PLAYER_ENTERING_WORLD");
end

SLASH_FLAMEWARDEN1, SLASH_FLAMEWARDEN2 = "/fw", "/fwarden";
SlashCmdList.FLAMEWARDEN = scanCalendar;